/* 导入axios工具包，这个模块封装 ‘首页模块’ 所有的ajax请求 */
import instance from '@/utils/axios.js'

//1.获取频道列表
const getChannels = () => {
  return instance({
    url: '/v1_0/user/channels',
    method: 'get'
  })
}
//导出接口
export { getChannels }

//2.获取文章列表
const getArticles = ({ channel_id, timestamp }) => {
  return instance({
    url: '/v1_0/articles',
    method: 'get',
    params: {
      with_top: 1, //是否需要置顶，给1即可
      channel_id,
      timestamp
    }
  })
}
//导出接口
export { getArticles }

//3.获取文章详细信息
const getArticleInfo = ({ id }) => {
  return instance({
    //这个接口的id直接在路径后面拼接  格式： url/id
    url: '/v1_0/articles/' + id,
    method: 'get'
  })
}
//导出接口
export { getArticleInfo }

//4.关注用户
const followUser = ({ target }) => {
  return instance({
    url: '/v1_0/user/followings',
    method: 'post',
    data: { target }
  })
}

//5.取消用户
const unFollowUser = ({ target }) => {
  return instance({
    //这个接口的id直接在路径后面拼接  格式： url/target
    url: '/v1_0/user/followings/' + target,
    method: 'delete'
  })
}
//导出接口
export { followUser, unFollowUser }

/* 6.点赞与取消点赞 */

//点赞
const addPraise = id => {
  return instance({
    method: 'post',
    url: '/v1_0/article/likings',
    data: {
      target: id
    }
  })
}

//取消点赞
const cancelPraise = id => {
  return instance({
    method: 'delete',
    //这个接口的id直接在路径后面拼接  格式： url/target
    url: '/v1_0/article/likings/' + id
  })
}

//导出接口
export { addPraise, cancelPraise }

/* 7.不喜欢与取消不喜欢 */

//不喜欢
const addDisLike = id => {
  return instance({
    method: 'post',
    url: '/v1_0/article/dislikes',
    data: {
      target: id
    }
  })
}

//取消不喜欢
const cancelDisLike = id => {
  return instance({
    method: 'delete',
    //这个接口的id直接在路径后面拼接  格式： url/target
    url: '/v1_0/article/dislikes/' + id
  })
}

//导出接口
export { addDisLike, cancelDisLike }

/* 8.搜索推荐词 */

const getSuggestion = keyword => {
  return instance({
    method: 'get',
    url: '/v1_0/suggestion',
    params: {
      q: keyword
    }
  })
}

//导出接口
export { getSuggestion }

/* 9.搜索列表 */

const getSearch = params => {
  return instance({
    method: 'get',
    url: '/v1_0/search',
    params
  })
}

//导出接口
export { getSearch }

/* 10.搜索历史 */
const getSearchHistory = () => {
  return instance({
    method: 'get',
    url: '/v1_0/search/histories',
  })
}

//导出接口
export { getSearchHistory }

/* 11.删除搜索历史 */
const deleteSearchHistory = () => {
  return instance({
    method: 'delete',
    url: '/v1_0/search/histories',
  })
}

//导出接口
export { deleteSearchHistory }